//
//  YAYIApilyServicepriceCell.m
//  YAYIDoctor
//
//  Created by hxp on 16/5/12.
//  Copyright © 2016年 hxp. All rights reserved.
//

#import "YAYIApilyServicepriceCell.h"

@implementation YAYIApilyServicepriceCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createViews];
    }
    return self;
}


-(void)setModel:(YAYIServiceModel *)model
{
    _model = model;
    [self reloadData];
}


-(void)reloadData
{
    if (_model == nil) {
        return;
    }
    
    CGFloat price = [_model.price floatValue] / 100;
    if ([_model isKindOfClass:[YAYIOrderServiceModel class]]) {
        
        YAYIOrderServiceModel *model = (YAYIOrderServiceModel *)_model;
        self.content.text = model.servicename;
        
        self.name.text = [NSString stringWithFormat:@"%.2f", price];
        self.textfield.text = [NSString stringWithFormat:@"%@", model.amount];
        
    }else{
        self.content.text = _model.title;
        self.name.text = [NSString stringWithFormat:@"%.2f", price];
        self.textfield.text = [NSString stringWithFormat:@"%@", self.model.amount];
    }
    
}


-(void)createViews
{
    
    // 单价
    self.title = [UILabel new];
    self.title.text = @"单价";
    self.title.font = [UIFont systemFontOfSize:font14];
    self.title.textColor = [UIColor blackColor];
    [self.contentView addSubview:self.title];
    
    self.content = [UILabel new];
    self.content.font = [UIFont systemFontOfSize:font14];
    self.content.textColor = [UIColor blackColor];
    [self.contentView addSubview:self.content];
    
    // 服务名
    self.name = [UILabel new];
    self.name.textAlignment = NSTextAlignmentRight;
    self.name.font = [UIFont systemFontOfSize:font14];
    self.name.textColor = [UIColor blackColor];
    [self.contentView  addSubview:self.name];
    
    self.addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.addBtn.frame = CGRectMake(0, 0, 24, 24);
    self.addBtn.tag = 0;
    [self.addBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.addBtn setImage:[UIImage imageNamed:@"addAmount"] forState:UIControlStateNormal];
    
    self.subBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.subBtn.tag = 1;
    self.subBtn.frame = CGRectMake(0, 0, 24, 24);
    self.subBtn.backgroundColor = [UIColor whiteColor];
    self.subBtn.layer.masksToBounds = YES;
    self.subBtn.backgroundColor = [UIColor whiteColor];
    [self.subBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.subBtn setImage:[UIImage imageNamed:@"subAmount"] forState:UIControlStateNormal];
    
    self.textfield = [UITextField new];
    self.textfield.text = @"1";
    self.textfield.textAlignment = NSTextAlignmentCenter;
    self.textfield.delegate = self;
    [self.contentView addSubview:self.textfield];
    
    self.deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.deleteBtn setImage:[UIImage imageNamed:@"deleteIcon"] forState:UIControlStateNormal];
    [self.contentView addSubview:self.deleteBtn];
    
    self.money = [UILabel new];
    self.money.textColor = [UIColor blueColor];
    self.money.font  = [UIFont systemFontOfSize:font14];
    [self.contentView addSubview:self.money];
    
    self.line = [UILabel new];
    self.line.backgroundColor = YAYIColor(0xdd, 0xdd, 0xdd);
    [self.contentView addSubview:self.line];

}

-(void)layoutSubviews
{
    [super layoutSubviews];

    [self.content mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(20));
        make.top.equalTo(@(12));
//        make.size.mas_equalTo(CGSizeMake(width, 20));
    }];
    
    [self.deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.content.mas_right).with.offset(4);
        make.centerY.mas_equalTo(self.content.mas_top).offset(2);
        make.size.mas_equalTo(CGSizeMake(22, 22));
    }];
    
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.top.mas_equalTo(self.content.mas_bottom).with.offset(12);
        make.size.mas_equalTo(CGSizeMake(SCREEN_width, 0.6));
    }];
    [self.title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(20));
        make.top.mas_equalTo(self.line.mas_bottom).with.offset(12);
        make.size.mas_equalTo(CGSizeMake(60, 20));
    }];
    self.name.textAlignment = NSTextAlignmentLeft;
    [self.name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.title.mas_right).with.offset(20);
        make.top.mas_equalTo(self.title.mas_top);
        make.size.mas_equalTo(CGSizeMake(150, 20));
    }];
    
    self.textfield.leftView = self.subBtn;
    self.textfield.rightView = self.addBtn;
    self.textfield.leftViewMode = UITextFieldViewModeAlways;
    self.textfield.rightViewMode = UITextFieldViewModeAlways;
    self.textfield.layer.borderColor = YAYIColor(212, 211, 211).CGColor;
    self.textfield.layer.borderWidth = 1;
    self.textfield.layer.cornerRadius = 13;
    [self.textfield mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.title.mas_top).with.offset(-3);
        make.right.mas_equalTo(self.contentView.mas_right).with.offset(-20);
        make.size.mas_equalTo(CGSizeMake(80, 26));
    }];

}


-(void)buttonAction:(UIButton *)sender
{
    int  count = [self.textfield.text intValue];
    if (sender.tag == 0) {
        count = count+1;
    }else{
        if (count == 1) {
            return;
        }
        count = count-1;
    }
    if (_delegate && [_delegate respondsToSelector:@selector(orderCount: price: model:)]) {
        [_delegate  orderCount:count price:_model.price model:_model];
    }
    self.textfield.text = [NSString stringWithFormat:@"%d",count];
}





- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    return NO;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

-(CGFloat)labelAutoWidth:(NSString *)str font:(UIFont *)font
{
    CGSize size = [str  sizeWithFont:[UIFont systemFontOfSize:font14] constrainedToSize:CGSizeMake(MAXFLOAT, 20) lineBreakMode:NSLineBreakByCharWrapping];
    return size.width;
}
@end
